package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("insuranceProduct")
public class InsuranceProductController {
    @Autowired
    private IInsuranceProductService insuranceProductService;
    @Autowired
    private IInsuranceService insuranceService;
    @Autowired
    private IInsuranceContentService insuranceContentService;

    // 分页查询及高级查询
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        qo.setOrderBy("ip.createTime DESC");
        // 分页查询及高级查询
        PageInfo pageInfo = insuranceProductService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        // 页面下拉框保险类型显示
        List<Insurance> insurances = insuranceService.listAll();
        model.addAttribute("insurances", insurances);
        return "insurance/product";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile file, InsuranceProduct entity) {// 上传图片，需要添加MultipartFile file
        // 上传图片，用到网络图片数据库：七牛
        if (file != null && file.getSize() > 0) {
            String upload = UploadUtil.uploadQiniu(file);
            entity.setCoverUrl(UploadUtil.QINIU_PATH + "/" + upload);
        }
        insuranceProductService.saveOrUpdate(entity);
        return new JSONResult();
    }

    // 添加修改回显模态框保险数据
    @RequestMapping("getContentById")
    @ResponseBody
    public InsuranceContent getContentById(Long id) {
        InsuranceContent insuranceContent = insuranceContentService.getContentById(id);
        return insuranceContent;
    }

    // 删除选中的保险产品
    @RequestMapping("delete")
    @ResponseBody
    public Object deleteById(Long id) {
        insuranceProductService.deleteById(id);
        return new JSONResult();
    }
}
